home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / rng / taus.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-05  |  3.7 KB  |  122 lines

  1. /* rng/taus.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <stdlib.h>
  22. #include <gsl/gsl_rng.h>
  23. #include <stdio.h>
  24.  
  25. /* This is a maximally equidistributed combined Tausworthe
  26.    generator. The sequence is,
  27.  
  28.    x_n = (s1_n ^ s2_n ^ s3_n) 
  29.  
  30.    s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  31.    s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  32.    s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  33.  
  34.    computed modulo 2^32. In the three formulas above '^' means
  35.    exclusive-or (C-notation), not exponentiation. Note that the
  36.    algorithm relies on the properties of 32-bit unsigned integers (it
  37.    is formally defined on bit-vectors of length 32). I have added a
  38.    bitmask to make it work on 64 bit machines.
  39.  
  40.    We initialize the generator with s1_1 .. s3_1 = s_n MOD m, where
  41.    s_n = (69069 * s_{n-1}) mod 2^32, and s_0 = s is the user-supplied
  42.    seed.
  43.  
  44.    The theoretical value of x_{10007} is 2733957125. The subscript
  45.    10007 means (1) seed the generator with s=1 (2) do six warm-up
  46.    iterations, (3) then do 10000 actual iterations.
  47.  
  48.    The period of this generator is about 2^88.
  49.  
  50.    From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  51.    Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  52.  
  53.    This is available on the net from L'Ecuyer's home page,
  54.  
  55.    http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  56.    ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps */
  57.  
  58. static inline unsigned long int taus_get (void *vstate);
  59. static double taus_get_double (void *vstate);
  60. static void taus_set (void *state, unsigned long int s);
  61.  
  62. typedef struct
  63.   {
  64.     unsigned long int s1, s2, s3;
  65.   }
  66. taus_state_t;
  67.  
  68. static inline unsigned long
  69. taus_get (void *vstate)
  70. {
  71.   taus_state_t *state = (taus_state_t *) vstate;
  72.  
  73. #define MASK 0xffffffffUL
  74. #define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) &MASK) ^ ((((s <<a) &MASK)^s) >>b)
  75.  
  76.   state->s1 = TAUSWORTHE (state->s1, 13, 19, 4294967294UL, 12);
  77.   state->s2 = TAUSWORTHE (state->s2, 2, 25, 4294967288UL, 4);
  78.   state->s3 = TAUSWORTHE (state->s3, 3, 11, 4294967280UL, 17);
  79.  
  80.   return (state->s1 ^ state->s2 ^ state->s3);
  81. }
  82.  
  83. static double
  84. taus_get_double (void *vstate)
  85. {
  86.   return taus_get (vstate) / 4294967296.0 ;
  87. }
  88.  
  89. static void
  90. taus_set (void *vstate, unsigned long int s)
  91. {
  92.   taus_state_t *state = (taus_state_t *) vstate;
  93.  
  94.   if (s == 0)
  95.     s = 1;    /* default seed is 1 */
  96.  
  97. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  98.   state->s1 = LCG (s);
  99.   state->s2 = LCG (state->s1);
  100.   state->s3 = LCG (state->s2);
  101.  
  102.   /* "warm it up" */
  103.   taus_get (state);
  104.   taus_get (state);
  105.   taus_get (state);
  106.   taus_get (state);
  107.   taus_get (state);
  108.   taus_get (state);
  109.   return;
  110. }
  111.  
  112. static const gsl_rng_type taus_type =
  113. {"taus",            /* name */
  114.  0xffffffffUL,            /* RAND_MAX */
  115.  0,                    /* RAND_MIN */
  116.  sizeof (taus_state_t),
  117.  &taus_set,
  118.  &taus_get,
  119.  &taus_get_double};
  120.  
  121. const gsl_rng_type *gsl_rng_taus = &taus_type;
  122.